package com.cloud.wms.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.core.http.BaseObserver;
import com.cloud.core.widget.dialog.CommonDialog;
import com.cloud.widget.utils.AnimationUtils;
import com.cloud.wms.R;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.tools.RxUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;

/**
 * @Author:Chen
 * @Date: 2021-03-29 14:39
 * @Description:
 */
public class TaskCompleteDialog extends CommonDialog {

    private final Context context;
    private ViewGroup llContainer;
    private TextView btnNext, btnClose;
    private OnNextListener onNextListener;

    public TaskCompleteDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected int resId() {
        return R.layout.dialog_task_complete;
    }

    @Override
    protected void initView() {
        super.initView();
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        lp.width = (int) (d.widthPixels * 0.9);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
    }

    @Override
    protected void initEvent() {
        llContainer = findViewById(R.id.ll_container);
        btnNext = findViewById(R.id.btn_next);
        btnClose = findViewById(R.id.btn_close);
        llContainer.startAnimation(AnimationUtils.getShowAlphaAnimation(200));
        btnClose.setOnClickListener(v -> TaskCompleteDialog.this.dismiss());
        setCancelable(false);
        this.setOnDismissListener(dialog -> ((Activity) context).finish());
        this.setOnShowListener(dialog -> {
            EventBusUtil.sendEvent(new Event<>(EventConfig.TASK_REFRESH));
//            Observable.timer(3000, TimeUnit.MILLISECONDS)
//                    .compose(RxUtils.threadSwitch())
//                    .subscribe(new BaseObserver<Long>() {
//                        @Override
//                        public void onSuccess(Long aLong) {
//                            TaskCompleteDialog.this.dismiss();
//                        }
//
//                        @Override
//                        public void onError(String msg) {
//
//                        }
//                    });
        });

        btnNext.setOnClickListener(v -> {
            if (onNextListener != null) {
                onNextListener.onNext();
            }
        });
    }

    public void setOnNextListener(OnNextListener onNextListener) {
        this.onNextListener = onNextListener;
        if (btnNext != null) {
            btnClose.setText("残忍拒绝");
            btnNext.setText("继续下一单");
            btnNext.setVisibility(View.VISIBLE);
        }
    }

    public void setOnNextListener(OnNextListener onNextListener, String btnText) {
        this.onNextListener = onNextListener;
        if (btnNext != null) {
            btnClose.setText("残忍拒绝");
            btnNext.setText(btnText);
            btnNext.setVisibility(View.VISIBLE);
        }
    }

    public interface OnNextListener {
        void onNext();
    }
}
